package com.neu.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.neu.common.JwtTokenUtils;
import com.neu.common.Result;
import com.neu.entity.Admin;
import com.neu.entity.Apply;
import com.neu.entity.Hire;
import com.neu.entity.Params;
import com.neu.exception.CustomException;
import com.neu.service.ApplyService;
import com.neu.service.HireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;
//    @Autowired


    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Apply> info = applyService.findBySearch(params);
        return Result.success(info);
    }

    @GetMapping("/findAll")
    public Result findAll(Params params){
        PageInfo<Apply> info = applyService.findByAll(params);
        return Result.success(info);
    }



    @PostMapping
    public Result add(@RequestBody Apply apply){

        Admin user = JwtTokenUtils.getCurrentUser();
        if(ObjectUtil.isNull(user)){
            throw new CustomException("从token中未解析到用户信息，请重新登录");
        }
        apply.setApplyId(user.getId());

        //2.往预订表里插入一条预订记录
        apply.setTime(DateUtil.now());
        apply.setStatus("待审核");
        applyService.add(apply);


        return Result.success();
    }

    @PostMapping("/audit")
    public Result audit(@RequestBody Apply apply){
        Admin user = JwtTokenUtils.getCurrentUser();
        if(ObjectUtil.isNull(user)){
            throw new CustomException("从token中未解析到用户信息，请重新登录");
        }
        apply.setAuditId(user.getId());
        applyService.audit(apply);
        return Result.success();
    }

//    @GetMapping("/getTutorInformation")
//    public Result getTutorInformation(@RequestBody Hire hire){
//        Admin tutor = hireService.getTutorInformation(hire);
//        return Result.success(tutor);
//    }

//    @PutMapping("/payment")
//    public Result payment(@RequestBody Hire hire){
//        hireService.payment(hire);
//        return Result.success();
//    }


//    @PutMapping("/interact")
//    public Result interact(@RequestBody Hire hire){
//        hireService.interact(hire);
//        return Result.success();
//    }

//    @DeleteMapping("/{id}")
//    public Result delete(@PathVariable Integer id){
//        hireService.delete(id);
//        return Result.success();
//    }
}
